import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/address_picker/address_picker_service.dart';
import 'package:x3_form_core/address_picker/model/address_picker_model.dart';
import 'package:x3_form_core/business_components/base_request_form_container.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';

class BaseRequestFormAddressDialog extends StatefulWidget {
  BaseRequestFormAddressDialog({Key key, @required this.request, this.requestSaveData}) : super(key: key);

  final BaseHttpRequestModel request;

  final RequestSaveData requestSaveData;
  @override
  _BaseRequestFormAddressDialogState createState() => _BaseRequestFormAddressDialogState();
}

class _BaseRequestFormAddressDialogState extends State<BaseRequestFormAddressDialog> {
  BaseRequestFormProvider _baseRequestFormProvider;
  AddressPickerModel _values;

  @override
  void didChangeDependencies() {
    /// 获取值
    _baseRequestFormProvider = BaseRequestFormProvider.of(context);
    _values = AddressPickerModel.fromJson(_baseRequestFormProvider.dataSource);
    super.didChangeDependencies();
  }

  @override
  Widget build(BuildContext context) {
    /// 显示的字符串
    return BaseRequestFormContainer(
      onPress: () => _openAddressPicker(),
      request: widget.request,
      showSuffixIcon: !_baseRequestFormProvider.isReadonly(widget.request),
      suffixIcon: BaseDropDownIcon(),
      child: _buildContent(),
    );
  }

  void _openAddressPicker() async {
    AddressPickerModel values = await AddressPicker.showAddressPicker(context, initData: _values);

    /// 判空处理
    if (values != null) {
      _values = values;
      _baseRequestFormProvider.setDataSource(widget.request, _values.toJson());

      /// 回调事件
      if (widget.requestSaveData != null) {
        widget.requestSaveData(widget.request, _baseRequestFormProvider.getMapDataSource(widget.request));
      }
      setState(() {});
    }
  }

  Widget _buildContent() {
    if (_values == null) return Text(widget.request.notes ?? '', style: Theme.of(context).inputDecorationTheme.hintStyle);
    return _buildSelectedAddress();
  }

  Widget _buildSelectedAddress() {
    return LayoutBuilder(
      builder: (BuildContext context, BoxConstraints constraints) {
        return Container(
          height: 48,
          width: constraints.maxWidth,
          alignment: Alignment.centerLeft,
          child: ListView(
            shrinkWrap: true,
            scrollDirection: Axis.horizontal,
            children: [
              _buildText(_values.province.name),
              SizedBox(width: 8),
              _buildText(_values.city.name),
              SizedBox(width: 8),
              _buildText(_values.area.name),
            ],
          ),
        );
      },
    );
  }

  Widget _buildText(String text) {
    return Container(alignment: Alignment.centerLeft, child: Text(text));
  }

  // Widget _buildSelectedAddress2() {
  //   return Container(
  //     padding: EdgeInsets.symmetric(vertical: 6),
  //     child: Column(
  //       crossAxisAlignment: CrossAxisAlignment.start,
  //       children: [
  //         Text(_values.province.name),
  //         Text(_values.city.name),
  //         Text(_values.area.name),
  //       ],
  //     ),
  //   );
  // }
}
